package com.propose.controller;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.propose.dao.ProposeDao;
import com.propose.domain.ProposeCommand;
import com.propose.validator.ProposeValidator;

@Controller
public class ProposeModifyController {

	@Autowired
	private ProposeDao proposeDao;
	
	@RequestMapping(value="/propose/proposeModify.do",method=RequestMethod.GET)
	public ModelAndView form(HttpSession session){
		String id = (String)session.getAttribute("id");
		ProposeCommand proposeCommand = proposeDao.selectPropose(id);
	
		String sex = proposeDao.getSex(id);
		System.out.println(sex);
		proposeCommand.setSex(sex);
		
		return new ModelAndView("proposeModify","proposeCommand",proposeCommand);
	}
	
	@RequestMapping(value="/propose/proposeModify.do",method=RequestMethod.POST)
	public String submit(ProposeCommand proposeCommand, BindingResult result, HttpSession session){

		System.out.println("zzzzzz");
		new ProposeValidator().validate(proposeCommand, result);
		if(result.hasErrors()){
			return "proposeModify";
		}
		
		String id = proposeCommand.getId();
		String pwd = proposeDao.getPassword(id);
		
		
		if(!proposeCommand.getPassword().equals(pwd)){
			result.rejectValue("password", "invalidPassword");
			return "proposeModify";
		}
		
		proposeDao.modifyPropose(proposeCommand);
		
		return "redirect:/member/myPage.do?id="+id;
	}
}
